home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / hdf / unix / hdf3_2r2.lha / HDF3.2r2 / util / hdftor8.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-28  |  13.7 KB  |  481 lines

  1. /***************************************************************************
  2. *
  3. *
  4. *                         NCSA HDF version 3.2r2
  5. *                            October 30, 1992
  6. *
  7. * NCSA HDF Version 3.2 source code and documentation are in the public
  8. * domain.  Specifically, we give to the public domain all rights for future
  9. * licensing of the source code, all resale rights, and all publishing rights.
  10. *
  11. * We ask, but do not require, that the following message be included in all
  12. * derived works:
  13. *
  14. * Portions developed at the National Center for Supercomputing Applications at
  15. * the University of Illinois at Urbana-Champaign, in collaboration with the
  16. * Information Technology Institute of Singapore.
  17. *
  18. * THE UNIVERSITY OF ILLINOIS GIVES NO WARRANTY, EXPRESSED OR IMPLIED, FOR THE
  19. * SOFTWARE AND/OR DOCUMENTATION PROVIDED, INCLUDING, WITHOUT LIMITATION,
  20. * WARRANTY OF MERCHANTABILITY AND WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE
  21. *
  22. ****************************************************************************
  23. */
  24.  
  25. #ifdef RCSID
  26. static char RcsId[] = "@(#)$Revision: 1.4 $";
  27. #endif
  28. /*
  29. $Header: /hdf/hdf/v3.2r2/util/RCS/hdftor8.c,v 1.4 1992/07/15 21:48:48 sxu beta koziol $
  30.  
  31. $Log: hdftor8.c,v $
  32.  * Revision 1.4  1992/07/15  21:48:48  sxu
  33.  *  Added changes for CONVEX
  34.  *
  35.  * Revision 1.3  1992/07/07  21:39:36  chouck
  36.  * Changed main() to be of type int and added PROTO() stuff
  37.  *
  38.  * Revision 1.2  1992/07/01  20:14:53  mlivin
  39.  * cleaned up some little things
  40.  *
  41.  * Revision 1.1  1992/06/30  20:46:56  mlivin
  42.  * Initial revision
  43.  *
  44.  * Revision 3.2  1991/10/22  17:56:10  dilg
  45.  * 5
  46.  * HDF3.1r5
  47.  *
  48.  * New machine types added:
  49.  *
  50.  *         PC      - IBM PC (DOS)
  51.  *         WIN     - IBM PC (Microsoft Windows 3.0)
  52.  *         IBM6000 - IBM RS/6000 (AIX)
  53.  *         CONVEX  - Convex C-2 (Unix)
  54.  *
  55.  * Bugs fixed in:
  56.  *
  57.  *         scup32.f
  58.  *         cspck32.f
  59.  *         dfpFf.f
  60.  *         dfpF.c
  61.  *         dfsd.c
  62.  *
  63.  * New utility added:
  64.  *
  65.  *         ristosds.c - convert raster images to sds.
  66.  *
  67.  * Also:
  68.  *         All code for the library was modified to conform to the
  69.  *         ANSI C standard.
  70.  *
  71.  * Revision 3.1  1990/07/02  10:11:37  clow
  72.  * some cosmetic modifications
  73.  *
  74. */
  75.  
  76. /*
  77.  * hdftor8.c
  78.  * Extract images from HDF file to raster files
  79.  */
  80.  
  81. #include "hdf.h"
  82. #include "herr.h"
  83.  
  84. #define PALETTE_SIZE    768    /* size of palette array */
  85. #define COLOR_SIZE    256    /* size of palette color array */
  86.                                 /* COLOR_SIZE == PALETTE_SIZE / 3 */
  87.  
  88. #define INTERACTIVE    'i'    /* interactive option */
  89. #define RASTER_FILE    'r'    /* raster filename template */
  90. #define PALETTE_FILE    'p'    /* palette filename template */
  91. #define VERBOSE        'v'    /* verbose option */
  92.  
  93. #define TEMPLATE_NUMBER    '#'    /* image or palette number positions */
  94. #define TEMPLATE_XDIM    '@'    /* image x dim positions */
  95. #define TEMPLATE_YDIM    '%'    /* image y dim positions */
  96.  
  97. #define D_RASTER_TEM    "img#-@.%" /* default raster file name template */
  98. #define D_PALETTE_TEM    "pal#"    /*  default palette file name template */
  99.  
  100. int interactive;        /* interactive option */
  101. int verbose;            /* verbose option */
  102.  
  103. int main
  104.     PROTO((int argc, char *argv[]));
  105. void putRaster
  106.     PROTO((char *template, int32 xdim, int32 ydim, int imageNumber,
  107.            uint8 *image));
  108. void putPalette
  109.     PROTO((char *template, int imageNumber, uint8 *palette));
  110. void convert
  111.     PROTO((char *template, int imageNumber, int32 xdim, int32 ydim,
  112.            char *stringOut));
  113. void fillStr
  114.     PROTO((char **template, char **stringOut, char *string, char specialChar));
  115. char *newSpace
  116.     PROTO((int32 size));
  117. char *getTemplate
  118.     PROTO((char *type, int imageNumber));
  119.  
  120.  
  121. #ifdef PROTOTYPE
  122. int main(int argc, char *argv[])
  123. #else
  124. int main(argc, argv)
  125.     int argc;
  126.     char *argv[];
  127. #endif /* PROTOTYPE */
  128. {
  129.     int i, imageNumber, ispal, err_val;
  130.     int32 xdim, ydim;
  131.     char *hdfFile;
  132.     uint8 *image, palette[PALETTE_SIZE];
  133.     char *rasterTemplate = NULL, *paletteTemplate = NULL;
  134.  
  135.     if (argc < 2) {
  136.         printf("%s,  version: 1.1   date: July 1, 1992\n", argv[0]);
  137.         printf("\tThis utility extracts all raster-8 images and/or\n");
  138.         printf("\tpalettes from an HDF file and stores them in two sets of\n");
  139.         printf("\tfiles containing only images and palettes, respectively.\n");
  140.         printf("Usage:\n");
  141.         printf("hdftor8 hdf_file [-i] [-v] [-r image_file] [-p pal_file]\n");
  142.         printf("\t-i: interactive (specify filenames interactively)\n");
  143.         printf("\t-v: verbose (provide descriptive messages)\n");
  144.         printf("\tImages and palettes are placed in the specified files\n");
  145.         printf("\tThe names of these files may contain special characters\n");
  146.         printf("\t\twhich will be replaced by numbers:\n");
  147.         printf("\t #    replace with image or palette number\n");
  148.         printf("\t @    replace with x dim of image\n");
  149.         printf("\t %%    replace with y dim of image\n");
  150.         printf("\tIf not specified, image filename defaults to img#-@.%%\n");
  151.         printf("\tIf not specified, palette filename defaults to pal.#\n\n");
  152.         exit(1);
  153.     }
  154.  
  155.     hdfFile = argv[1];
  156.  
  157.     for (i = 2; i < argc; i++) {
  158.         if (*argv[i] == '-') {
  159.             switch (argv[i][1]) {
  160.             case INTERACTIVE :
  161.                 interactive = TRUE;
  162.                 break;
  163.             case RASTER_FILE :
  164.                 rasterTemplate = argv[++i];
  165.                 break;
  166.             case PALETTE_FILE :
  167.                 paletteTemplate = argv[++i];
  168.                 break;
  169.             case VERBOSE :
  170.                 verbose = TRUE;
  171.                 break;
  172.             default :
  173.                 printf("Illegal option: %s, skipping...\n", argv[i]);
  174.                 break;
  175.             }
  176.         }
  177.         else
  178.             printf("Illegal option: %s, skipping...\n", argv[i]);
  179.     }
  180.  
  181.     if (!rasterTemplate && !interactive)
  182.         rasterTemplate = D_RASTER_TEM;
  183.     if (!paletteTemplate && !interactive)
  184.         paletteTemplate = D_PALETTE_TEM;
  185.  
  186.     for(imageNumber = 1;!DFR8getdims(hdfFile, &xdim, &ydim, &ispal);) {
  187.         image = (uint8 *)newSpace(xdim * ydim);
  188.         if (verbose) {
  189.             if (ispal)
  190.                 printf("Getting image and palette %d.\n", imageNumber);
  191.             else
  192.                 printf("Getting image %d.\n", imageNumber);
  193.             printf("Image dimensions : %ld * %ld\n", xdim, ydim);
  194.         }
  195.         if (!DFR8getimage(hdfFile, image, xdim, ydim, palette)) {
  196.             putRaster(rasterTemplate, xdim, ydim, imageNumber, image);
  197.             if(ispal) putPalette(paletteTemplate, imageNumber, palette);
  198.             imageNumber++;
  199.         }
  200.         else break;
  201.     }
  202.  
  203.     err_val = HEvalue(1);
  204.     if ((err_val != DFE_NOMATCH) && (err_val != DFE_NONE)) {
  205.         if (verbose) HEprint(stderr, 0);
  206.         exit(1);
  207.     }
  208. }
  209.  
  210. /*
  211.  * putRaster
  212.  *
  213.  * Write the image to a raster image file.
  214.  *
  215.  * INPUT:
  216.  *        template : pointer to template string
  217.  *        xdim : x dimension of image
  218.  *        ydim : y dimension of image
  219.  *        imageNumber : (need I say more?)
  220.  *        image : pointer to image array
  221.  */
  222. #ifdef PROTOTYPE
  223. void putRaster(char *template, int32 xdim, int32 ydim, int imageNumber,
  224.            uint8 *image)
  225. #else
  226. void putRaster(template, xdim, ydim, imageNumber, image)
  227.     char *template, *image;
  228.     int32 xdim, ydim;
  229.     int imageNumber;
  230. #endif /* PROTOTYPE */
  231. {
  232.     FILE *fd;
  233.     char fileName[DF_MAXFNLEN];
  234.  
  235.  
  236.     if (!template)                /* can assume interactive (see main) */
  237.         template = (char *)getTemplate("image", imageNumber);
  238.  
  239.     convert(template, imageNumber, xdim, ydim, fileName);
  240.  
  241.     if (verbose)
  242.         printf("Writing into image file : %s\n",fileName);
  243.  
  244. #ifdef PC
  245.     if ((fd = fopen(fileName, "wb")) == NULL) {
  246.         puts("Unable to open file. Exiting...");
  247.         exit(1);
  248.     }
  249. #else
  250.     if ((fd = fopen(fileName, "w")) == NULL) {
  251.         puts("Unable to open file. Exiting...");
  252.         exit(1);
  253.     }
  254. #endif
  255.     if (fwrite(image, (int)xdim, (int)ydim, fd) != (unsigned)ydim) {
  256.         puts("Unable to write to file. Exiting...");
  257.         exit(1);
  258.     }
  259.     if (fclose(fd)) {
  260.         puts("Unable to close file. Exiting...");
  261.          exit(1);
  262.     }
  263. }
  264.  
  265. /*
  266.  * putPalette
  267.  *
  268.  * Write palette array out to palette file.
  269.  *
  270.  * INPUT:
  271.  *        template : palette filename template
  272.  *        imageNumber : Yes, the number of the image
  273.  *        palette : pointer to the palette array
  274.  */
  275. #ifdef PROTOTYPE
  276. void putPalette(char *template, int imageNumber, uint8 *palette)
  277. #else
  278. void putPalette(template, imageNumber, palette)
  279.     char *template, *palette;
  280.     int imageNumber;
  281. #endif /* PROTOTYPE */
  282. {
  283.     int i;
  284.     FILE *fd;
  285.     char fileName[DF_MAXFNLEN], reds[COLOR_SIZE];
  286.     char greens[COLOR_SIZE], blues[COLOR_SIZE];
  287.  
  288.     if (!template)        /* can assume interactive (see main) */
  289.         template = (char *)getTemplate("palette", imageNumber);
  290.  
  291.     convert(template, imageNumber, (int32) 1, (int32) 768, fileName);
  292.  
  293.     if (verbose)
  294.         printf("Writing into palette file : %s\n", fileName);
  295.  
  296. #ifdef PC
  297.     if ((fd = fopen(fileName, "wb")) == NULL) {
  298.         puts("Unable to open file. Exiting...");
  299.         exit(1);
  300.     }
  301. #else
  302.     if ((fd = fopen(fileName, "w")) == NULL) {
  303.         puts("Unable to open file. Exiting...");
  304.         exit(1);
  305.     }
  306. #endif
  307.  
  308.     for(i = 0; i < COLOR_SIZE; i++) {
  309.         reds[i] = *palette++;
  310.         greens[i] = *palette++;
  311.         blues[i] = *palette++;
  312.     }
  313.     if (fwrite(reds, 1, COLOR_SIZE, fd) != COLOR_SIZE) {
  314.         printf("Unable to write to file. Exiting...");
  315.         exit(1);
  316.     }
  317.     if (fwrite(greens, 1, COLOR_SIZE, fd) != COLOR_SIZE) {
  318.         printf("Unable to write to file. Exiting...");
  319.         exit(1);
  320.     }
  321.     if (fwrite(blues, 1, COLOR_SIZE, fd) != COLOR_SIZE) {
  322.         printf("Unable to write to file. Exiting...");
  323.         exit(1);
  324.     }
  325.     if (fclose(fd)) {
  326.         printf("Unable to close file. Exiting...");
  327.         exit(1);
  328.     }
  329. }
  330.  
  331. /*
  332.  * convert
  333.  *
  334.  * Determine the file name given the template, imageNumber, x dimension
  335.  * and y dimension. Replaces template special characters with the
  336.  * corresponding numbers.
  337.  *
  338.  * INPUT:
  339.  *        template : file name template
  340.  *        imageNumber :
  341.  *        xdim : x dimension of image
  342.  *        ydim : y dimension of image
  343.  * OUTPUT:
  344.  *        stringOut : the concocted file name
  345.  */
  346. #ifdef PROTOTYPE
  347. void convert(char *template, int imageNumber, int32 xdim, int32 ydim,
  348.          char *stringOut)
  349. #else
  350. void convert(template, imageNumber, xdim, ydim, stringOut)
  351.     char *template, *stringOut;
  352.     int imageNumber;
  353.     int32 xdim, ydim;
  354. #endif /* PROTOTYPE */
  355. {
  356.     char numStr[20], xStr[20], yStr[20];
  357.  
  358.     sprintf(numStr, "%1d", imageNumber);
  359.     sprintf(xStr, "%1ld", xdim);
  360.     sprintf(yStr, "%1ld", ydim);
  361.  
  362.     for(;(*template);) {
  363.         switch (*template) {
  364.         case TEMPLATE_NUMBER :
  365.             fillStr(&template, &stringOut, numStr, TEMPLATE_NUMBER);
  366.             break;
  367.         case TEMPLATE_XDIM :
  368.             fillStr(&template, &stringOut, xStr, TEMPLATE_XDIM);
  369.             break;
  370.         case TEMPLATE_YDIM :
  371.             fillStr(&template, &stringOut, yStr, TEMPLATE_YDIM);
  372.             break;
  373.         default :
  374.             *stringOut++ = *template++;
  375.         }
  376.     }
  377.     *stringOut = '\0';
  378. }
  379.  
  380. /*
  381.  * fillStr
  382.  *
  383.  * Fill a string of special characters with a number string.
  384.  * If the number string is shorter than the number of special characters
  385.  * then the string is padded with '0' on the left. Else the number of the
  386.  * special characters is ignored.
  387.  *
  388.  * INPUT:
  389.  *        template : pointer to pointer of string template
  390.  *        string : pointer to the number string
  391.  *        specialChar : the special character we are replacing
  392.  * OUTPUT:
  393.  *        stringOut : pointer to pointer of converted string (not really)
  394.  * BUG: Both the pointer to the template string and the pointer to the
  395.  *        comverted string are moved to after the position of the conversion.
  396.  */
  397. #ifdef PROTOTYPE
  398. void fillStr(char **template, char **stringOut, char *string, char specialChar)
  399. #else
  400. void fillStr(template, stringOut, string, specialChar)
  401.     char **template, **stringOut, *string, specialChar;
  402. #endif /* PROTOTYPE */
  403. {
  404.     int templateLen, stringLen, i;
  405.  
  406.     for(templateLen = 1; *(++(*template)) == specialChar; templateLen++);
  407.     stringLen = strlen(string);
  408.  
  409.     for(i = templateLen - stringLen; i > 0; i--)
  410.         *(*stringOut)++ = '0';
  411.  
  412.     for(;(*string);) *(*stringOut)++ = *string++;
  413. }
  414.  
  415. /*
  416.  * newSpace
  417.  *
  418.  * Allocate a space with little wastage
  419.  *
  420.  * INPUT:
  421.  *        size : size of space request
  422.  * RETURN:
  423.  *        pointer to the space allocated
  424.  *
  425.  * BUG: This routine can only handle one request at any time,
  426.  *        a second call cannot be made while the space is still
  427.  *        in use (somewhere else).
  428.  */
  429. #ifdef PROTOTYPE
  430. char *newSpace(int32 size)
  431. #else
  432. char *newSpace(size)
  433.     int32 size;
  434. #endif /* PROTOTYPE */
  435. {
  436.     static int32 oldSize = 0;        /* must be static */
  437.     static char *oldSpace = NULL; /* must be static */
  438.  
  439.     if (size >= oldSize) {
  440.         if (oldSpace != NULL) HDfreespace(oldSpace);
  441.         if ((oldSpace = (char *) HDgetspace((uint32) size)) == NULL) {
  442.             puts("Out of memory. Abort.");
  443.             exit(1);
  444.         }
  445.         oldSize = size;
  446.     }
  447.  
  448.     return oldSpace;
  449. }
  450.  
  451. /*
  452.  * getTemplate
  453.  *
  454.  * Ask the user for a file name template string.
  455.  *
  456.  * INPUT:
  457.  *        type : a description string of the type of file,
  458.  *                i.e. image or palette.
  459.  *        imageNumber :
  460.  * RETURN:
  461.  *        pointer to template string
  462.  * BUG: This routine can only handle one request at any time,
  463.  *        a second call cannot be made while the template is still
  464.  *        in use (somewhere else).
  465.  */
  466. #ifdef PROTOTYPE
  467. char *getTemplate(char *type, int imageNumber)
  468. #else
  469. char *getTemplate(type, imageNumber)
  470.     int imageNumber;
  471.     char *type;
  472. #endif /* PROTOTYPE */
  473. {
  474.     static char template[DF_MAXFNLEN];
  475.  
  476.     printf("This is %s %d.\nWhat template would you like?\n",
  477.            type, imageNumber);
  478.     scanf("%s", template);
  479.     return template;
  480. }
  481.